﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;

namespace Handrise.Mobile.BasicModal.Config
{
    public class ConfigService 
    {
        private string configFileName = "AppConfig.xml";

        private XmlDocument xmlDoc = new XmlDocument();

        public ConfigService()
        {
            try
            {
                xmlDoc.Load(GetLocalPath() + "\\" + configFileName);
            }
            catch
            {
                
            }
        }

        private string GetLocalPath()
        {
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

            if (path.StartsWith("file:\\"))
                return path.Remove(0, 6);
            else
                return path;
        }

        /// <summary>
        /// 获取默认的节点位置
        /// </summary>
        /// <returns></returns>
        private string GetDefaultNodePath()
        {
            return "Configuration/" + System.Reflection.Assembly.GetCallingAssembly().GetName().Name + "/";
        }

        public string GetParameter(string nodePath, string attributeName)
        {
            if (xmlDoc == null)
                return "";
            
            XmlNode xmlNode = GetXmlNode(nodePath);

            if (xmlNode == null)
                xmlNode = GetXmlNode(GetDefaultNodePath() + nodePath);

            if (xmlNode != null)
            {
            
                try
                {
                    return xmlNode.Attributes[attributeName].Value;
                }
                catch
                {
                }

            }

            return "";
        }


        /// <summary>
        /// 设置单一项的值
        /// </summary>
        /// <param name="nodePath"></param>
        /// <param name="attributeName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetValue(string nodePath, string attributeName, string value)
        {
            if (xmlDoc == null)
                return false ;

            XmlNode xmlNode = GetXmlNode(nodePath);

            if (xmlNode == null)
                xmlNode = GetXmlNode(GetDefaultNodePath() + nodePath);

            if (xmlNode != null)
            {

                try
                {
                    xmlNode.Attributes[attributeName].Value = value ;
                }
                catch
                {
                }

            }

            xmlDoc.Save(GetLocalPath() + "\\" + configFileName);
            return true;
        }

        private XmlNode GetXmlNode(string nodePath)
        {
            if (xmlDoc == null)
                return null;

            XmlNode nodtReturn = null;
            try
            {
                //根据指定路径获取节点
                nodtReturn = xmlDoc.SelectSingleNode(nodePath);

            }
            catch
            {

            }
            return nodtReturn;
        }
    }
}
